package com.wdz.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.wdz.ArticleApplication;
import com.wdz.http.Response;
import com.wdz.utils.ApplicationContextUtil;
import com.wdz.utils.RedisFieldName;
import com.wdz.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private RedisUtil redisUtil;
    @GetMapping("/")
    public Response get(){
        //RedisUtil redisUtil = (RedisUtil) ApplicationContextUtil.getBean("RedisUtil");
        String rn = RedisFieldName.INDEXDASHBOARD;
        Map<Object,Object> data = redisUtil.hmget(rn);
        if(data.isEmpty()){
            ArticleApplication articleApplication = (ArticleApplication) ApplicationContextUtil.getBean("ArticleApplication");
            articleApplication.setIndexDash();
            data = redisUtil.hmget(rn);
        }
        //转换json
        //JSONObject jsonObject = JSONUtil.parseObj(data.get("7tq"));
        //Set<String> key = jsonObject.keySet();
       // key.forEach(item->{
            //jsonObject.put(item,jsonObject.getStr(item).substring(1,jsonObject.getStr(item).length()-1));
        //});
        //data.put("7tq", jsonObject);
        return Response.success(data);
    }
}
